﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;

namespace TireService.Web
{
    public partial class LatLngUpdate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (Page.IsPostBack != true)
            //{
            //    TireServiceDB db = new TireServiceDB();

            //    ASPxGridView1.DataSource = db.Tasks.Where(p => p.PrimaryLatitude == 0 & p.PrimaryLongitude == 0 & p.Createdate >= Convert.ToDateTime("2013-09-01")).ToList();
            //    ASPxGridView1.DataBind();

            //}
            //TireServiceDB db = new TireServiceDB();
            //ASPxGridView1.DataSource = db.ServiceCarScheduleDetails.Where(p => p.ToLatitude == 0 & p.ToLongitude == 0).ToList();
            //ASPxGridView1.DataBind();
        }

        protected void ASPxButton1_Click(object sender, EventArgs e)
        {
            TireServiceDB db = new TireServiceDB();

            List<ServiceCarScheduleDetail> details = db.ServiceCarScheduleDetails.Where(p => p.ToLatitude == 0 & p.ToLongitude == 0).ToList();

            foreach (ServiceCarScheduleDetail detail in details)
            {
                // Calculate longitude and latitude for address
                try
                {
                    string primaryAddress = detail.ToAddress + ", " + detail.ToZipCode + " " + detail.ToCity + ", Danmark";
                    //string primaryAddress = task.PrimaryZipCode + " " + task.PrimaryCity + ", Danmark";
                    //GoogleMaps googleMapAPI = new GoogleMaps("ABQIAAAAOqg0zdZdLRm2WH8lJpFDLxRBYr_GQowC8JJyghooYbwZzCFRqRS3_8Vqwz9Fzn2YR5-Z-atDW-OUxg");
                    GoogleMaps googleMapAPI = new GoogleMaps("AIzaSyBZMHncOTQg7gJGD2SVhrloJtNmxMtF4k0");
                    double[] LongLatPrimary = googleMapAPI.GetLongLatCoordinates(primaryAddress);
                    detail.ToLongitude = LongLatPrimary[0];
                    detail.ToLatitude = LongLatPrimary[1];
                }
                catch (Exception ex)
                {
                    string t = ex.Message;
                }
                
            }

            db.SubmitChanges();

            ASPxGridView1.DataSource = db.ServiceCarScheduleDetails.Where(p => p.ToLatitude == 0 & p.ToLongitude == 0).ToList();
            ASPxGridView1.DataBind();

            Response.Write("20 opdateret");
        }
    }
}